package org.celery.mobile.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.celery.common.config.WxMaConfiguration;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

/**
 * Create on 2021-11-22
 *
 * @author Celery <1031868928@qq.com>
 */
@RestController
@AllArgsConstructor
@RequestMapping("/mobile/wechat/{appid}")
public class MobileWechatController {

    @PostMapping(value = "greet")
    public R<WxMaJscode2SessionResult> greet(@PathVariable String appid, String jsCode) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appid);
        try {
            return R.data(wxService.jsCode2SessionInfo(jsCode));
        } catch (WxErrorException e) {
            throw new ServiceException("微信登录失败");
        }
    }
}
